import crypto from 'crypto'
// AES 加密
function encrypt(text, secretKey) {
  const iv = crypto.randomBytes(16) // 初始化向量
  const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(secretKey), iv)
  let encrypted = cipher.update(text, 'utf8', 'hex')
  encrypted += cipher.final('hex')
  return iv.toString('hex') + ':' + encrypted
}
export function genRegCode(userId, durationInDays, secretKey) {
  // const secretKey = 'yilongchen1yilongchen2yilongchen' //用于HMAC的密钥
  const payload = `${userId}*${durationInDays}*${Date.now()}`
  const hash = crypto.createHmac('sha256', secretKey).update(payload).digest('hex')
  const payload_aes = encrypt(payload, secretKey)
  return `${payload_aes}--${hash}`
}

// console.log(genRegCode('zhangsan', 30))
